package avicit.bdp.dds.api.service;

import avicit.bdp.dds.dispatch.enums.ZKNodeType;
import avicit.bdp.dds.dispatch.model.Server;
import avicit.bdp.dds.dispatch.model.WorkerServerModel;
import avicit.bdp.dds.api.utils.ZookeeperMonitor;
import avicit.bdp.dds.common.Constants;
import avicit.bdp.dds.dao.MonitorDBDao;
import avicit.bdp.dds.dao.entity.MonitorRecord;
import avicit.bdp.dds.dao.entity.ZookeeperRecord;
import com.google.common.collect.Sets;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import static avicit.bdp.dds.common.utils.Preconditions.checkNotNull;

/**
 * monitor service
 */
@Service
public class MonitorService {

    @Autowired
    private ZookeeperMonitor zookeeperMonitor;

    @Autowired
    private MonitorDBDao monitorDBDao;

    /**
     * query database state
     *
     * @return data base state
     */
    public Map<String, Object> queryDatabaseState() {
        Map<String, Object> result = new HashMap<>(5);

        List<MonitorRecord> monitorRecordList = monitorDBDao.queryDatabaseState();

        result.put(Constants.DATA_LIST, monitorRecordList);

        return result;

    }

    /**
     * query master list
     *
     * @return master information list
     */
    public Map<String, Object> queryMaster() {

        Map<String, Object> result = new HashMap<>(5);

        List<Server> masterServers = getServerListFromZK(true);
        result.put(Constants.DATA_LIST, masterServers);

        return result;
    }

    /**
     * query zookeeper state
     *
     * @return zookeeper information list
     */
    public Map<String, Object> queryZookeeperState() {
        Map<String, Object> result = new HashMap<>(5);

        List<ZookeeperRecord> zookeeperRecordList = zookeeperMonitor.zookeeperInfoList();

        result.put(Constants.DATA_LIST, zookeeperRecordList);

        return result;

    }

    /**
     * query worker list
     *
     * @return worker information list
     */
    public Map<String, Object> queryWorker() {

        Map<String, Object> result = new HashMap<>(5);
        List<WorkerServerModel> workerServers = getServerListFromZK(false)
                .stream()
                .map((Server server) -> {
                    WorkerServerModel model = new WorkerServerModel();
                    model.setId(server.getId());
                    model.setHost(server.getHost());
                    model.setPort(server.getPort());
                    model.setZkDirectories(Sets.newHashSet(server.getZkDirectory()));
                    model.setResInfo(server.getResInfo());
                    model.setCreateTime(server.getCreateTime());
                    model.setLastHeartbeatTime(server.getLastHeartbeatTime());
                    return model;
                })
                .collect(Collectors.toList());

        Map<String, WorkerServerModel> workerHostPortServerMapping = workerServers
                .stream()
                .collect(Collectors.toMap(
                        (WorkerServerModel worker) -> {
                            String[] s = worker.getZkDirectories().iterator().next().split("/");
                            return s[s.length - 1];
                        }
                        , Function.identity()
                        , (WorkerServerModel oldOne, WorkerServerModel newOne) -> {
                            oldOne.getZkDirectories().addAll(newOne.getZkDirectories());
                            return oldOne;
                        }));

        result.put(Constants.DATA_LIST, workerHostPortServerMapping.values());

        return result;
    }

    List<Server> getServerListFromZK(boolean isMaster) {

        checkNotNull(zookeeperMonitor);
        ZKNodeType zkNodeType = isMaster ? ZKNodeType.MASTER : ZKNodeType.WORKER;
        return zookeeperMonitor.getServersList(zkNodeType);
    }

}
